package com.photovoltaic.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.photovoltaic.auth.entity.OperationLog;
import com.photovoltaic.auth.mapper.OperationLogMapper;
import com.photovoltaic.auth.service.OperationLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {
    
    @Override
    public List<OperationLog> findByUserIdAndTimeBetween(Long userId, LocalDateTime start, LocalDateTime end) {
        LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OperationLog::getUserId, userId)
                .ge(OperationLog::getCreateTime, start)
                .le(OperationLog::getCreateTime, end)
                .orderByDesc(OperationLog::getCreateTime);
        return list(queryWrapper);
    }

    @Override
    public List<OperationLog> findByOperationType(String operationType) {
        LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OperationLog::getOperationType, operationType)
                .orderByDesc(OperationLog::getCreateTime);
        return list(queryWrapper);
    }

    @Override
    public void recordLog(Long userId, String operationType, String description, String ip, Integer status) {
        OperationLog log = new OperationLog();
        log.setUserId(userId);
        log.setOperationType(operationType);
        log.setOperationDesc(description);
        log.setIpAddress(ip);
        log.setStatus(status);
        save(log);
    }

}